package com.sgb.member.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.member.BR;
import com.sgb.member.R;
import com.sgb.member.entity.MemberClientListBean;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * Time:2021/8/27
 * AUTHOR:F_Q
 * Description:
 */
public class MemberClientAdapter extends BaseRecycleAdapter<MemberClientListBean> {
    public MemberClientAdapter(Context context, List<MemberClientListBean> datas) {
        super(context, R.layout.item_member_client, datas);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        TextView edit = holder.itemView.findViewById(R.id.edit);//编辑
        TextView delete = holder.itemView.findViewById(R.id.delete);//删除
        TextView companyname = holder.itemView.findViewById(R.id.company_name);//企业名称
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemonclicks.Edit(position);
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemonclicks.Delete(datas.get(position).getNo());
            }
        });
        companyname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemonclicks.CompanyWeb(datas.get(position).getCompanyNo());
            }
        });
    }

    @Override
    public void convert(ItemViewHolder holder, MemberClientListBean entity) {
        holder.setBinding(BR.viewModel, entity);
        TextView businesstype = holder.itemView.findViewById(R.id.business_type);
        TextView servicescope = holder.itemView.findViewById(R.id.service_scope);
        TextView industrytype = holder.itemView.findViewById(R.id.industry_type);
        //行业类型一级
        if (!TextUtils.isEmpty(entity.getTradeTypeFirst())) {
            industrytype.setText(entity.getTradeTypeFirst());
        }
        //行业类型二级
        if (!TextUtils.isEmpty(entity.getTradeTypeSecond())) {
            String content =industrytype.getText().toString();
            industrytype.setText(content + "/" + entity.getTradeTypeSecond());
        }
        List<MemberClientListBean.businessTypeBean> businessType = entity.getBusinessType();
        if (businessType!=null&&businessType.size()>0){
            StringBuilder sb = new StringBuilder();
            for (MemberClientListBean.businessTypeBean businessTypeBean : businessType) {
                sb.append(businessTypeBean.getName()).append("、");
            }
            String businessText = sb.substring(0, sb.toString().length() - 1);
            businesstype.setText(businessText);
        }
        List<MemberClientListBean.serviceRangeBean> serviceRange = entity.getServiceRange();
        if (businessType!=null&&businessType.size()>0) {
            StringBuilder sb = new StringBuilder();
            for (MemberClientListBean.serviceRangeBean serviceRangeBean : serviceRange) {
                sb.append(serviceRangeBean.getName()).append("、");
            }
            String servicetext = sb.substring(0, sb.toString().length() - 1);
            servicescope.setText(servicetext);
        }
    }

    private ItemOnClick itemonclicks;

    public void setItemOnClick(ItemOnClick itemOnClick) {
        itemonclicks = itemOnClick;
    }

    public interface ItemOnClick{
        void Edit(int pos);
        void Delete(String no);
        void CompanyWeb(String companyNo);
    }
}
